package com.cskaoyan.controller.wx_coupon;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wx_coupon.*;
import com.cskaoyan.bean.wx_coupon.vo.WxCouponMyListVo;
import com.cskaoyan.service.wx_coupon.WxCouponService;
import com.cskaoyan.service.wx_coupon.WxCouponUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: Jason 805636901@qq.com
 * @create: 2021-10-22-10:26 AM
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;
    @Autowired
    WxCouponUserService wxCouponUserService;

    //http://182.92.235.201:8083/wx/coupon/list?page=1&limit=10
    @GetMapping("list")
    //TODO 校验参数,异常处理.limit,page
    public BaseRespVo list(@NotNull Integer page, @NotNull Integer limit) {
        WxCouponPageData data = wxCouponService.queryWxCouponList(page, limit);
        return BaseRespVo.ok(data);
    }

    //我的优惠券
    @GetMapping("mylist")
    public BaseRespVo myList(HttpServletRequest httpServletRequest, int status, int page, int limit) {
        //String token = httpServletRequest.getSession().getId(); 获取token:61e8b321-a0fd-4fe2-8aef-3dea1805646d(String)
        User user = (User) SecurityUtils.getSubject().getPrincipals()
                .getPrimaryPrincipal();//获取已登录的用户信息.

        WxCouponMyListVo wxCouponMyListVo = wxCouponService.queryMyCouponBySatus(status, page, user.getId());
        wxCouponMyListVo.setLimit(limit);
        return BaseRespVo.ok(wxCouponMyListVo);
    }

    //兑换优惠券
    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map) {
        User user = (User) SecurityUtils.getSubject().getPrincipals()
                .getPrimaryPrincipal();//获取已登录的用户信息.

        String code = (String) map.get("code");
        int flag = wxCouponService.validCode(code, user.getId());
        if (flag == 1) return BaseRespVo.ok("成功领取");//
        return BaseRespVo.error();
    }

    /*领取优惠券*/
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map map) {


        User user = (User) SecurityUtils.getSubject().getPrincipals()
                .getPrimaryPrincipal();//获取已登录的用户信息.
        if (user.getId() == null) return BaseRespVo.error("请登录");

        Integer id = (Integer) map.get("couponId");
        if (id == null) return BaseRespVo.error("参数有误");

        WxCoupon wxCoupon = wxCouponService.findById(id);
        if (wxCoupon.getId() == null) {
            return BaseRespVo.error("未找到该优惠券");
        }

        //用户领券
        WxCouponUser couponUser = new WxCouponUser();
        couponUser.setCouponId(id);
        couponUser.setUserId(user.getId());

        Date startTime = new Date();
        Calendar c = Calendar.getInstance();
        Date endTime = new Date();

        c.setTime(startTime);
        c.add(Calendar.DATE, 3); //截至日期为3天后
        endTime = c.getTime();

        couponUser.setStartTime(startTime);
        couponUser.setEndTime(endTime);
        wxCouponUserService.add(couponUser);
        return BaseRespVo.ok();

    }

    /*当前购物车下单商品订单可用优惠券*/
    @GetMapping("selectlist")
    //TODO:...
    public BaseRespVo selectlist(Integer cartId, Integer grouponRulesId) {
        User user = (User) SecurityUtils.getSubject().getPrincipals()
                .getPrimaryPrincipal();//获取已登录的用户信息.
        if (user.getId() == null) {
            return BaseRespVo.error("请登录");
        }

        //WxCouponMyListVo wxCouponMyListVo = wxCouponService.queryMyAvailableCoupon((user.getId()));
        List<WxCouponInfo> WxCouponInfos = wxCouponService.queryMyAvailableCoupon1(cartId, grouponRulesId);
        return BaseRespVo.ok(PageDataVo.ok(WxCouponInfos));
    }
}
